"""
抽屉组件示例
"""
import sys
import os

# 添加项目根目录到Python路径
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout, 
                            QHBoxLayout, QPushButton, QLabel, QLineEdit, 
                            QComboBox, QSpacerItem, QSizePolicy)
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont

from component.drawer import Drawer
from component.theme import Theme
from component.button import Button

class MainWindow(QMainWindow):
    """抽屉组件示例程序的主窗口"""
    
    def __init__(self):
        super().__init__()
        
        # 设置窗口属性
        self.setWindowTitle("抽屉组件示例")
        self.resize(800, 600)
        
        # 创建中央部件
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        
        # 创建主布局
        main_layout = QVBoxLayout(central_widget)
        main_layout.setContentsMargins(40, 40, 40, 40)
        main_layout.setSpacing(20)
        
        # 标题
        title = QLabel("抽屉组件示例")
        title.setFont(QFont("Arial", 24, QFont.Bold))
        main_layout.addWidget(title)
        
        # 描述
        description = QLabel("抽屉组件可以从屏幕的四个方向滑出，用于展示临时内容或表单。")
        description.setWordWrap(True)
        main_layout.addWidget(description)
        
        # 按钮容器
        button_container = QWidget()
        button_layout = QVBoxLayout(button_container)
        button_layout.setContentsMargins(0, 20, 0, 0)
        button_layout.setSpacing(20)
        
        # 方向按钮组
        directions = [
            ("右侧抽屉", "right"),
            ("左侧抽屉", "left"),
            ("顶部抽屉", "top"),
            ("底部抽屉", "bottom")
        ]
        
        for label, direction in directions:
            button_row = QWidget()
            row_layout = QHBoxLayout(button_row)
            row_layout.setContentsMargins(0, 0, 0, 0)
            
            # 创建按钮
            button = Button(label, button_type="primary")
            button.setProperty("direction", direction)
            button.clicked.connect(self.open_drawer)
            
            row_layout.addWidget(button)
            row_layout.addStretch()
            
            button_layout.addWidget(button_row)
        
        main_layout.addWidget(button_container)
        main_layout.addStretch()
    
    def open_drawer(self):
        """打开抽屉"""
        sender = self.sender()
        direction = sender.property("direction")
        
        self._open_drawer(direction)
    
    def _open_drawer(self, direction):
        """打开抽屉"""
        print(f"打开抽屉，方向: {direction}")
        
        # 创建抽屉
        if direction in ["right", "left"]:
            drawer = Drawer(
                parent=self,
                direction=direction,
                title=f"{direction.capitalize()}侧抽屉",
                drawer_width=350,
                drawer_height=self.height()
            )
        else:  # top, bottom
            drawer = Drawer(
                parent=self,
                direction=direction,
                title=f"{direction.capitalize()}侧抽屉",
                drawer_width=self.width(),
                drawer_height=300
            )
        
        # 添加表单内容
        self._add_form_content(drawer)
        
        # 显示抽屉
        drawer.show()
    
    def _add_form_content(self, drawer):
        """向抽屉添加表单内容"""
        # 添加描述文本
        description = QLabel("这是一个抽屉组件示例，您可以在这里放置任何内容，如表单、列表或详情信息。")
        description.setWordWrap(True)
        drawer.content_layout.addWidget(description)
        
        # 添加间隔
        drawer.content_layout.addSpacing(20)
        
        # 添加表单元素
        form_items = [
            ("用户名", QLineEdit()),
            ("邮箱", QLineEdit()),
            ("角色", QComboBox())
        ]
        
        # 设置下拉框选项
        form_items[2][1].addItems(["管理员", "编辑", "访客"])
        
        for label_text, widget in form_items:
            item_container = QWidget()
            item_layout = QVBoxLayout(item_container)
            item_layout.setContentsMargins(0, 0, 0, 0)
            item_layout.setSpacing(8)
            
            # 标签
            label = QLabel(label_text)
            item_layout.addWidget(label)
            
            # 输入控件
            item_layout.addWidget(widget)
            
            drawer.content_layout.addWidget(item_container)
            drawer.content_layout.addSpacing(10)
        
        # 添加按钮
        button_container = QWidget()
        button_layout = QHBoxLayout(button_container)
        button_layout.setContentsMargins(0, 20, 0, 0)
        
        # 取消按钮
        cancel_button = Button("取消")
        cancel_button.clicked.connect(drawer.close_with_animation)
        
        # 确认按钮
        submit_button = Button("提交", button_type="primary")
        submit_button.clicked.connect(drawer.close_with_animation)
        
        button_layout.addWidget(cancel_button)
        button_layout.addSpacing(10)
        button_layout.addWidget(submit_button)
        
        drawer.content_layout.addWidget(button_container)
        drawer.content_layout.addStretch()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    
    # 设置应用样式
    app.setStyleSheet(Theme().get_style_sheet())
    
    window = MainWindow()
    window.show()
    
    sys.exit(app.exec_()) 